package com.xj.admin.bussiness.registerLicense.service.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.xj.common.bussiness.deviceInformationImg.entity.TbDeviceInformationImg;
import com.xj.common.bussiness.deviceInformationImg.mapper.TbDeviceInformationImgMapper;
import com.xj.common.bussiness.personalInformationImg.entity.TbPersonalInformationImg;
import com.xj.common.bussiness.personalInformationImg.mapper.TbPersonalInformationImgMapper;
import com.xj.common.bussiness.registerLicense.entity.TbRegisterLicense;
import com.xj.common.bussiness.registerLicense.entity.registerLicenseMould;
import com.xj.common.bussiness.registerLicense.mapper.TbRegisterLicenseMapper;
import com.xj.admin.bussiness.registerLicense.service.ITbRegisterLicenseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xj.common.bussiness.transferLicense.entity.TbTransferLicense;
import com.xj.common.bussiness.transferLicense.mapper.TbTransferLicenseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hmemb
 * @since 2018-01-12
 */
@Service
public class TbRegisterLicenseServiceImpl extends ServiceImpl<TbRegisterLicenseMapper, TbRegisterLicense> implements ITbRegisterLicenseService {

    @Autowired
    private TbRegisterLicenseMapper registerLicenseMapper;

    @Autowired
    private TbTransferLicenseMapper transferLicenseMapper;

    @Autowired
    private TbPersonalInformationImgMapper personalInformationImgMapper;

    @Autowired
    private TbDeviceInformationImgMapper deviceInformationImgMapper;

    @Override
    public TbRegisterLicense createTransferLicenseToForm(String factoryNum) {
        if(factoryNum != null && !factoryNum.equals("")){
            TbRegisterLicense tbrl = new TbRegisterLicense();
            tbrl.setFactoryNum(factoryNum);
            List<TbRegisterLicense> registerLicensesList = registerLicenseMapper.selectList(Condition.create().eq("factory_num", factoryNum));
            List<TbTransferLicense> transferLicenseList = transferLicenseMapper.selectList(Condition.create().eq("factory_num", factoryNum));
            if(transferLicenseList.size()>0){
                return null;
            }else{
                if(registerLicensesList.size()>0 && registerLicensesList.size()<2){
                    TbRegisterLicense rl = new TbRegisterLicense();
                    for (TbRegisterLicense registerLicenses:registerLicensesList) {
                        rl=registerLicenses;
                        break;
                    }
                    return rl;
                }else{
                    return null;
                }
            }
        }else{
            return null;
        }

    }

    /**
     * 根据身份证号查询身份证照片
     * @param identityNum
     * @return
     */
    @Override
    public TbPersonalInformationImg searchPersonalInformationImg(String identityNum) {
        if(!identityNum.isEmpty()){
            List<TbPersonalInformationImg> list = personalInformationImgMapper.selectList(Condition.create().eq("identity_num",identityNum));
            if(list.size()< 1 || list.size()>1){
                return null;
            }
            TbPersonalInformationImg img = new TbPersonalInformationImg();
            for (TbPersonalInformationImg personalInformationImg: list) {
                img = personalInformationImg;
            }
            return img;
        }
        return null;
    }

    @Override
    public TbDeviceInformationImg searchDeviceInformationImg(String identityNum, String bigShelfNum) {
        if(!identityNum.isEmpty()){
            List<TbDeviceInformationImg> list = deviceInformationImgMapper.selectList(Condition.create().eq("identity_num",identityNum).eq("big_shelf_num",bigShelfNum));
            if(list.size() > 1 || list.size()<1 ){
                return null;
            }
            TbDeviceInformationImg img = new TbDeviceInformationImg();
            for (TbDeviceInformationImg deviceInformationImg: list) {
                img = deviceInformationImg;
            }
            return img;
        }
        return null;
    }

    @Override
    public List<registerLicenseMould> searchRegisterLicenseList(TbRegisterLicense registerLicense) {
        return registerLicenseMapper.searchRegisterLicenseList(registerLicense);
    }
}
